const db=require('../utils/mysql')
const { v4: uuidv4 } = require('uuid');
module.exports={
    noticeAdd:async (ctx,next)=>{
        let query=ctx.request.body
        let noticeId=uuidv4()
        let result= await new Promise((resolve,reject)=>{
            const sql=`INSERT INTO notice (noticeId,noticetitle,noticecontent,noticestatus,noticetime) 
           values ('${noticeId}','${query.noticetitle}','${query.noticecontent}','${query.noticestatus=='发布'?1:0}','${query.noticetime}')`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'公告添加成功',
                    status:200
                })
            })
        })
        ctx.body=result
    },
    noticeEdit:async (ctx,next)=>{
        let query=ctx.request.body
        let result= await new Promise((resolve,reject)=>{
            const sql=`update notice set 
            noticetitle='${query.noticetitle}',noticecontent='${query.noticecontent}',noticestatus='${query.noticestatus=='下架'?0:1}',noticetime='${query.noticetime}'
            where noticeId='${query.noticeId}'`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'公告修改成功',
                    status:200
                })
            })
        })
        ctx.body=result
    },
    noticeDelete:async (ctx,next)=>{
        let query=ctx.request.query
        let result= await new Promise((resolve,reject)=>{
            const sql=`delete from notice where noticeId='${query.noticeId}'`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'公告删除成功',
                    status:200
                })
            })
        })
        ctx.body=result
    },
    getNoticeList:async (ctx,next)=>{
        let query=ctx.request.query
        let sql=`select * from notice`
        if (query.noticestatus){
            sql=`select * from notice where noticestatus='${query.noticestatus}'`
        }
        let result= await new Promise((resolve,reject)=>{
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'公告列表获取成功',
                    status:200,
                    data:data
                })
            })
        })
        ctx.body=result

    },
    getNoticeInfo:async (ctx,next)=>{
        let query=ctx.request.query
        let result= await new Promise((resolve,reject)=>{
            const sql=`select * from notice where noticeId='${query.noticeId}'`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'公告详情获取成功',
                    status:200,
                    data:data
                })
            })
        })
        ctx.body=result

    }
}

